/**************************************************************************//**
*
* @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date            Author       Notes
* 2020-11-11      Wayne        First version
*
******************************************************************************/

#ifndef __DRV_I2C_H__
#define __DRV_I2C_H__

#include <rtthread.h>
#include <rthw.h>
#include <rtdevice.h>
#include "nuc970_i2c.h"
#include "drv_sys.h"

typedef struct _i2c_config
{
    I2C_TypeDef *I2C;
    const char *bus_name;
    rt_tick_t timeout;
    IRQn_Type irq_type;
} i2c_config;
/* i2c dirver class */
struct rt_soft_i2c_bus
{
    struct rt_i2c_bus_device i2c_bus;
};

#define SYS_CLOCK_PCLK                  75000   // kHz

#define I2C_MSTA_START                  0x08UL
#define I2C_MSTA_REPEAT_START           0x10UL
#define I2C_MSTA_T_ADDR_ACK             0x18UL
#define I2C_MSTA_T_ADDR_NACK            0x20UL
#define I2C_MSTA_T_DATA_ACK             0x28UL
#define I2C_MSTA_T_DATA_NACK            0x30UL
#define I2C_MSTA_ARBITRATION_LOST       0x38UL
#define I2C_MSTA_R_ADDR_ACK             0x40UL
#define I2C_MSTA_R_ADDR_NACK            0x48UL
#define I2C_MSTA_R_DATA_ACK             0x50UL
#define I2C_MSTA_R_DATA_NACK            0x58UL
#define I2C_MSTA_BUS_ERROR              0x00UL
#define I2C_MSTA_BUS_RELEASED           0xF8UL

#define I2C_SSTA_T_REPEAT_START_OR_STOP 0xA0UL
#define I2C_SSTA_T_ADDR_ACK             0xA8UL
#define I2C_SSTA_T_DATA_NACK            0xC0UL
#define I2C_SSTA_R_ADDR_ACK             0x60UL
#define I2C_SSTA_R_DATA_ACK             0x80UL

#if defined(BSP_USING_I2C0)
#define I2C0_BUS_CONFIG                                 \
    {                                                   \
        .I2C = I2C0,                                    \
        .bus_name = "i2c0",                             \
        .timeout = 100,                                 \
        .irq_type = I2C0_IRQn,                               \
    }
#endif
#if defined(BSP_USING_I2C1)
#define I2C1_BUS_CONFIG                                 \
    {                                                   \
        .I2C = I2C1,                                    \
        .bus_name = "i2c1",                             \
        .timeout = 100,                                 \
        .irq_type = I2C1_IRQn,                               \
    }
#endif

#endif /* __DRV_I2C_H__ */
